﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace LibarayMS
{
    public partial class Frm_login : Form
    {
        private string mid;
        private string mname;
        public Frm_login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txt_UserName.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户名！");
                txt_UserName.Focus();
            }
            else if (txt_Password.Text.Trim() == "")
            {
                MessageBox.Show("请输入密码！");
                txt_Password.Focus();
            }
            else
            {
                MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                conn.Open();
                string str = string.Format("select MID,MName from manager_info where UserName='{0}' and Password='{1}'",
                    txt_UserName.Text, txt_Password.Text);
                MySqlDataAdapter da = new MySqlDataAdapter(str, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                if (dt.Rows.Count == 1)
                {
                    foreach(DataRow dr in dt.Rows)
                    {
                        mid = dr["MID"].ToString();
                        mname = dr["MName"].ToString();
                    }
                    Frm_main frm = new Frm_main(mname,mid);
                    frm.logoutEvent += new myDelegate(Frm_logoutEvent);
                    frm.Show();
                    clearTextBox();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("对不起，用户名或密码不正确！");
                }
            }
        }

        private void clearTextBox()
        {
            txt_UserName.Text = "";
            txt_Password.Text = "";
        }

        private void Frm_logoutEvent()
        {
            this.Show();
        }
    }
}
